package sv.mecca.modelo;

import java.sql.SQLException;
import java.util.ArrayList;
import sv.mecca.modelo.Conexion;
import sv.mecca.form.ExamenesForm;

public class OperacionesExamenes extends Conexion{

    public void insertarExamenes(ExamenesForm ex) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("insert into examenes(expediente_paciente, gb, n, l, hb, ht, plt, tgo, tgp, coombs, proteina, anatinina) values(" +
                "'" + ex.getExpediente() + "', " +
                "" + ex.getGb()+ ", " +
                "" + ex.getN()+ ", " +
                "" + ex.getL()+ ", " +
                "" + ex.getHb()+ ", " +
                "" + ex.getHt()+ ", " +
                "" + ex.getPlt()+ ", " +
                "" + ex.getTgo()+ ", " +
                "" + ex.getTgp()+ ", " +
                "'" + ex.getCoombs()+ "', " +
                "" + ex.getProteina()+ ", " +
                "" + ex.getAnatinina()+ ")");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        ex.destroy();
        }
    public ExamenesForm modificarExamenes(Integer id) throws SQLException{
        ExamenesForm ex = new ExamenesForm();
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from examenes where id = " + id);
            rs.next();
            ex.setId(rs.getInt(1));
            ex.setExpediente(rs.getString(2));
            ex.setGb(rs.getDouble(3));
            ex.setN(rs.getDouble(4));
            ex.setL(rs.getDouble(5));
            ex.setHb(rs.getDouble(6));
            ex.setHt(rs.getDouble(7));
            ex.setPlt(rs.getDouble(8));
            ex.setTgo(rs.getDouble(9));
            ex.setTgp(rs.getDouble(10));
            ex.setCoombs(rs.getString(11));
            ex.setProteina(rs.getDouble(12));
            ex.setAnatinina(rs.getDouble(13));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            rs.close();
            st.close();
            cn.close();
            return ex;
            }
        }
    public void actualizarExamenes(ExamenesForm ex, Integer id) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.executeUpdate("update examenes set " +
                "expediente_paciente = '" + ex.getExpediente() + "', " +
                "gb = " + ex.getGb()+ ", " +
                "n = " + ex.getN()+ ", " +
                "l = " + ex.getL()+ ", " +
                "hb = " + ex.getHb()+ ", " +
                "ht = " + ex.getHt()+ ", " +
                "plt = " + ex.getPlt()+ ", " +
                "tgo = " + ex.getTgo()+ ", " +
                "tgp = " + ex.getTgp()+ ", " +
                "coombs = '" + ex.getCoombs()+ "', " +
                "proteina = " + ex.getProteina()+ ", " +
                "anatinina = " + ex.getAnatinina()+ " " +
                "where id = " + id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        ex.destroy();
        }
    public void EliminarExamenes(Integer id) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("delete from examenes where id = " + id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        }
    public ArrayList<ExamenesForm> listado() throws SQLException{
        ArrayList<ExamenesForm> examenes = null;
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from examenes");
            examenes = new ArrayList<ExamenesForm>();
            while (rs.next()) {
                ExamenesForm ex = new ExamenesForm(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getDouble(4), rs.getDouble(5), rs.getDouble(6), rs.getDouble(7), rs.getDouble(8), rs.getDouble(9), rs.getDouble(10), rs.getString(11), rs.getDouble(12), rs.getDouble(13));
                examenes.add(ex);
                }
            rs.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            return examenes;
            }
        }
    }